package com.yt.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.order.models.bo.WorkOrderEvaluationSaveBO;
import com.yt.order.models.dto.WorkOrderInfoDTO;
import com.yt.order.models.service.IWorkOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import com.yt.order.models.service.IWorkOrderEvaluationService;
import com.yt.order.models.bo.WorkOrderEvaluationBO;
import com.yt.order.models.dto.WorkOrderEvaluationDTO;

import java.util.List;

/**
 * 工单评论表控制器
 *
 * @author zhou
 * @Date 2025-09-14 10:03:32
 */

@RestController
@RequestMapping("/api/workOrderEvaluation")
@Slf4j
@Api(value = "WorkOrderEvaluationApiController",tags = "工单评论表Api接口")
public class WorkOrderEvaluationApiController {

    @DubboReference
    private IWorkOrderEvaluationService workOrderEvaluationService;
    @DubboReference
    private IWorkOrderInfoService workOrderInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<WorkOrderEvaluationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        WorkOrderEvaluationDTO dto = workOrderEvaluationService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "工单评论表列表",notes = "工单评论表列表")
    @PostMapping("/queryPageList")
    public Result<Page<WorkOrderEvaluationDTO>> queryPageList(WorkOrderEvaluationBO bo){
        Wrapper<WorkOrderEvaluationDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<WorkOrderEvaluationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = workOrderEvaluationService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @PostMapping("/evaluation")
    @ApiOperation(value = "评价",notes = "评价")
    public Result<Boolean> getEvaluation(@RequestBody WorkOrderEvaluationSaveBO SaveBo){
        if(SaveBo == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Wrapper<WorkOrderInfoDTO>wrapper=new EntityWrapper<>();
        wrapper.eq("id", SaveBo.getWorkOrderId());
        WorkOrderInfoDTO workOrderInfoDTO=workOrderInfoService.selectOne(wrapper);
        //工单状态为1已完成且未评价时才可以评价
        if(workOrderInfoDTO.getStatus()==1 && workOrderInfoDTO.getIsEvaluation()==1) {
            //修改工单评论状态
            workOrderInfoDTO.setIsEvaluation(0);
            workOrderInfoService.updateById(workOrderInfoDTO);
            //插入数据库
            Boolean flag = workOrderEvaluationService.add(SaveBo);
            return new Result<>(flag);
        }
        return new Result<>(CodeEnum.FAIL_100002 );
    }
    @PostMapping("/autoEvaluation")
    @ApiModelProperty("自动评价")
    @Scheduled(cron = "0 0 2 * * ?")//每日凌晨2点触发
    public Result autoEvaluation(){
        //查询已完成未评价的工单
        Wrapper<WorkOrderInfoDTO> wrapper=new EntityWrapper<>();
        wrapper.eq("status", 1)
                .eq("is_evaluation", 1);
        List<WorkOrderInfoDTO> list=workOrderInfoService.selectList(wrapper);
        if(list.size()>0){
            for(WorkOrderInfoDTO workOrderInfoDTO:list){
                //将工单的数据赋给评价表中
                WorkOrderEvaluationDTO workOrderEvaluationDTO=workOrderEvaluationService.addEva(workOrderInfoDTO);
                workOrderEvaluationService.insert(workOrderEvaluationDTO);
            }
        }
        return new Result<>();
    }
}
